#ifndef convert_H
#define convert_H

#include"unistd.h"
#include"stdio.h"
#include"iostream"
#include"stdlib.h"
#include"fstream"
#include"string.h"

using namespace std;

#define version_ "	version 1.0"
#define size 100
#define capacity 15

/*
	name:version
	function:show version
	return: void
	*/
	void version()
{
	cout<<version_<<endl;
}

//help function
void help()
{
	cout<<"Usage: convert [options] file..."<<endl \
	<<"Options:"<<endl \
	<<"	--help						Display this information"<<endl \
	<<"	-glade	para1|para2|...		convert glade file into std-glade file"<<endl \
	<<"	para1|para2...				default convert glade file"<<endl \
	<<"	-version					show version"<<endl;
}

/*
	name string2char
	function: transform string to char*
	return 
	*/
void string2char(string string_, char **char_)
{
	*char_ = (char*)malloc(string_.length()+1);
	
	int i = 0;
	for(; i<string_.length(); i++)
		(*char_)[i] = string_[i];
	(*char_)[i] = '\0';
}

/*
	name:get_pwd
	function: get current work directory
	return: directory if sucess else return NULL
	*/
char *get_pwd()
{
	static char pwd[size]={0};
	if(getcwd(pwd, size-1) == NULL)
	{
		perror("Error:	can't get current work directory!");
		return NULL;
	}
	return pwd;
}
/*
	name: filename
	function: get filename from input
	return: char*
	*/
char *filename(int argc, char **argv)
{
	for(int i = 1; i<argc; i++)
	{
		if(**(argv+i) == '-')
			continue;
		return *(argv+i);
	}
}

int isExist(char *file_name)
{
	fstream file;
	file.open(file_name, ios::in);
	if(!file)
	{
		perror("Error:	");
		return 0;
	}
	else
		return 1;
}
/*
	name: isLegal
	function: cheack whether input string is legal
	return: int
		0	convert parameters found
		1	missing parameters
		-1	version or help
		2	file is no exist
		3	unexpected error
	*/
int isLegal(int argc, char **argv)
{
// -1 / 0 is normal
	switch(argc)
	{
		case 1:		//missing parameters
			perror("Error:	one or more parameter is missing!");
			perror("type:	'convert -h' for help");
			return 1;
		
		case 2:
			//for help
			if(strcmp(*(argv+1), "--h") == 0)
			{
				help();
				return -1;
			}
			else if(strcmp(*(argv+1), "-version") == 0)
			{
				version();
				return -1;
			}
			else if(**(argv+1) != '-')
				if(isExist(*(argv+1)) == 0)
					return 2;
				else
					return 0;
			else
			{
				perror("Unexpect error founded!");
				return 3;
			}
			
		default:
			int i;
			if(strcmp(*(argv+1), "-glade") == 0)
				i = 2;
			else
				i = 1;
			for(; i<argc; i++)
			{
				if(isExist(*(argv+i)) == 0)
					return 2;
			}
			return 0;
	}
} 

/*
	name: modifyfile
	function: the main function to modify glade files
	return: void
	*/
void modifyfile(char *pwd, char *file_name)
{
	FILE *fp = NULL, *swp = NULL;
	char c;
	char head[] = "<glade-interface>";
	char tail[] = "</glade-interface>";
	int counter = 0;	//record number of '\n'
	char *pwd_swp = NULL;
	
	string file_dir = (string)pwd + "/" + file_name;
	string swp_dir = file_dir + ".swp";
	
	string2char(file_dir, &pwd);
	string2char(swp_dir, &pwd_swp);
	
	if((fp = fopen(pwd,"r")) == NULL)
	{
		perror("Error:	can't open file");
	}
	cout<<"	creating swap file of "<<'"'<<file_name<<'"'<<endl;
	if((swp = fopen(pwd_swp,"w")) == NULL)
	{
		perror("Error:	can't open file");
	}
	
	while(1)
	{
		while((c = fgetc(fp)) != '\n')
		{
			if(c == EOF)
			{
				//fseek(swp, -14, SEEK_END);
				for(int i = 0; i<strlen(tail); i++)
					fputc(tail[i], swp);
				fclose(fp);
				fclose(swp);
				
				string delete_ = (string)"rm "+file_dir;
				string rename_ = (string)"cp " +swp_dir +" "+ file_dir;
				char *command_del, *command_rename;
				string2char(delete_, &command_del);
				string2char(rename_, &command_rename);
				
				cout<<"	swap file..."<<endl;
				system(command_del);
				system(command_rename);
				cout<<"	Done!"<<endl;
				return;
			}
			fputc(c, swp);
		}
		
		fputc('\n', swp);
		counter++;
		if(counter == 2)
		{
			for(int i = 0; i<strlen(head); i++)
			{
				putc(head[i], swp);
			}
			fputc('\n', swp);
		}
	}
}


#endif
